<template>
  <div class="search_form">
    <div class="select_title">筛选</div>
    <!-- 模板、名称、工艺版次、作者、状态、创建时间 -->
    <el-form
      label-position="top"
      label-width="80px"
      :model="filterData"
      size="small"
      class="form"
      v-init-screen-h="270"
    >
      <el-form-item label="零件号：">
        <el-select
          class="w100"
          clearable
          v-model="filterData.partNumber"
          collapse-tags
          filterable
          placeholder="请选择"
          size="mini"
          value-key="token"
        >
          <el-option
            v-for="item in tableDataUniqBy('partNumber')"
            :key="item.partNumber"
            :label="item.partNumber"
            :value="item.partNumber"
          >
          </el-option>
        </el-select>
        <!-- <el-input
          v-model="filterData.partNumber"
          clearable
          size="mini"
          placeholder="请输入零件号"
        ></el-input> -->
      </el-form-item>
      <el-form-item label="工序号：">
        <el-select
          class="w100"
          clearable
          v-model="filterData.processNumber"
          collapse-tags
          filterable
          placeholder="请选择"
          size="mini"
          value-key="token"
        >
          <el-option
            v-for="item in tableDataUniqBy('processNumber')"
            :key="item.processNumber"
            :label="item.processNumber"
            :value="item.processNumber"
          >
          </el-option>
        </el-select>
        <!-- <el-input
          v-model="filterData.processNumber"
          clearable
          size="mini"
          placeholder="请输入工序号"
        ></el-input> -->
      </el-form-item>
      <el-form-item label="模板：">
        <el-select
          class="w100"
          clearable
          v-model="filterData.templateId"
          collapse-tags
          filterable
          placeholder="请选择"
          size="mini"
          value-key="token"
        >
          <el-option
            v-for="item in templateListUniqBy"
            :key="item.token"
            :label="item.name + '-' + item.version"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="工艺版次：">
        <el-select
          class="w100"
          clearable
          v-model="filterData.version"
          collapse-tags
          filterable
          placeholder="请选择"
          size="mini"
          value-key="token"
        >
          <el-option
            v-for="item in tableDataUniqBy('version')"
            :key="item.version"
            :label="item.version"
            :value="item.version"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="状态：">
        <el-select
          class="w100"
          clearable
          v-model="filterData.state"
          collapse-tags
          filterable
          placeholder="请选择"
          size="mini"
          value-key="token"
        >
          <el-option label="可用" value="true"> </el-option>
          <el-option label="禁用" value="false"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="日期：">
        <div class="input_date">
          <el-date-picker
            v-model="filterData.startDate"
            type="date"
            size="mini"
            placeholder="开始日期"
          ></el-date-picker>
          <i class="el-icon-right" style="color: rgba(0, 0, 0, 0.25)"></i>
          <el-date-picker
            v-model="filterData.endDate"
            type="date"
            size="mini"
            placeholder="结束日期"
          ></el-date-picker>
        </div>
      </el-form-item>
      <el-form-item label="创建人：">
        <el-select
          class="w100"
          clearable
          v-model="filterData.author"
          collapse-tags
          filterable
          placeholder="请选择"
          size="mini"
        >
          <el-option
            v-for="item in userList"
            :key="item.username"
            :label="item.username"
            :value="item.username"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <div class="search_btns">
      <el-button class="btn" size="mini" @click="reSet">重置</el-button>
      <el-button class="btn" size="mini" @click="submitForm" type="primary"
        >提交</el-button
      >
    </div>
  </div>
</template>

<script>
import _ from "lodash";
export default {
  props: {
    templateList: {
      type: Array,
      default: () => [],
    },
    userList: {
      type: Array,
      default: () => [],
    },
    tableData: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      filterData: {
        version: "",
        author: "",
        partNumber: "",
        startDate: "",
        endDate: "",
        templateId: "",
        state: "",
        processNumber: "",
      },
    };
  },
  computed: {
    templateListUniqBy() {
      return _.uniqBy(this.templateList, "name");
    },
    tableDataUniqBy() {
      return function (key = "processNumber") {
        return _.uniqBy(this.tableData, key);
      };
    },
  },
  mounted() {},
  methods: {
    //重置
    reSet() {
      for (const key in this.filterData) {
        this.filterData[key] = "";
      }
      // this.filterData = {
      //   version: "",
      //   author: "",
      //   partNumber: "",
      //   startDate: "",
      //   endDate: "",
      //   templateId: "",
      //   version: "",
      //   state: "",
      //   processNumber: "",
      // };
      this.$emit("setFilterList");
    },
    submitForm() {
      if (
        this.filterData.version ||
        this.filterData.author ||
        this.filterData.partNumber ||
        this.filterData.startDate ||
        this.filterData.endDate ||
        this.filterData.templateId ||
        this.filterData.state ||
        this.filterData.processNumber
      ) {
        this.$emit("setFilterList", this.filterData);
      } else {
        this.$emit("setFilterList");
      }
    },
  },
};
</script>

<style lang='scss' scoped>
.search_form {
  width: 25%;
  margin-left: 10px;
  padding: 18px 0;
  padding-bottom: 0;
  background-color: #fff;
  position: relative;
  .input_date {
    display: flex;
    align-items: center;
  }
  .form {
    padding: 0 12px;
  }
  /* padding-bottom: 80px; */
  /* box-sizing: border-box; */
  .search_btns {
    /* position: absolute; */
    /* bottom: 20px; */
    left: 0;
    width: 100%;
    display: flex;
    padding: 12px;
    padding-bottom: 0;
    .btn {
      width: 50%;
    }
  }
  /deep/ .el-form-item {
    margin-bottom: 5px;
    .el-form-item__label {
      padding: 0;
    }
  }
  .select_title {
    font-size: 16px;
    color: #333;
    font-weight: 600;
    margin-bottom: 20px;
    margin-left: 12px;
  }
}
</style>